package com.MinesweeperGame.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;


public class Scores {
	
	Label name = new Label("Winner's Name :");
	TextBox textboxname = new TextBox();
	
	Label comments = new Label("comments");
	TextBox textboxcomments = new TextBox();
	
	Button buttonsubmit = new Button("Submit Score");
	Button buttondonotsubmit = new Button("Do not Submit");
	
	Label youwin = new Label("You Win!");
	Label congrats = new Label("Congratulations on winning GWT MineSweeper!");
	
	PopupPanel popuppanel_win = new PopupPanel();
	
	private int gameTime;
	
	private final GameServiceAsync gameService = GWT
			.create(GameService.class);
	

	public Scores(int noOfClicks, int time, String level ) {
		
		AbsolutePanel absolutepanel = new AbsolutePanel();
		
		gameTime = time;
		
		Label gametime = new Label("Game Time:"+time);
		Label bestbeginnertime = new Label("Your best Beginner time is:" +time );
		Label bestintermediatetime = new Label("Your best Intermediate time is:" +time);
		Label bestexperttime = new Label("Your best Expert time is:" +time);
		
		Label clicks = new Label("No of clicks:"+ noOfClicks);
		
		if(level.equals("beginner")) {
			bestintermediatetime.setVisible(false);
			bestexperttime.setVisible(false);
		}
		if(level.equals("intermediate")) {
			bestbeginnertime.setVisible(false);
			bestexperttime.setVisible(false);
		}
		if(level.equals("expert")) {
			bestintermediatetime.setVisible(false);
			bestbeginnertime.setVisible(false);
		}
		buttondonotsubmit.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				popuppanel_win.hide();
			}
		});
	
		buttonsubmit.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				
				addScore();
				popuppanel_win.hide();
			}
		});
		
		absolutepanel.add(youwin);
		absolutepanel.add(congrats);
		absolutepanel.add(gametime);
		absolutepanel.add(bestbeginnertime);
		absolutepanel.add(bestintermediatetime);
		absolutepanel.add(bestexperttime);
		absolutepanel.add(clicks);
		absolutepanel.add(name);
		absolutepanel.add(textboxname);
		absolutepanel.add(comments);
		absolutepanel.add(textboxcomments);
		absolutepanel.add(buttonsubmit);
		absolutepanel.add(buttondonotsubmit);
		
		popuppanel_win.setStyleName("score");
		youwin.setStyleName("labelWin");
		congrats.setStyleName("labelCongrats");
		buttonsubmit.setStyleName("centerAlign");
		buttondonotsubmit.setStyleName("centerAlign");
		popuppanel_win.add(absolutepanel);
		popuppanel_win.center(); 
		popuppanel_win.show();
		
		}
	


	public void addScore() {
		
		gameService.addToScore(gameTime, new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				
				System.out.print("error");
			}

			public void onSuccess(String result) {
				Window.alert("Time has been Uploaded");
				System.out.println("Time has been Uploaded");
			}
		});
		
	}
}

